package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsTopic;
import com.qf.service.AdminTopicService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/admin/topic")
@CrossOrigin
public class AdminTopicController {

    @Autowired
    private AdminTopicService adminTopicService;

    /**
     * 专题列表
     * @param page
     * @param limit
     * @param title
     * @param subtitle
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:topic:list")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "专题列表")
    public Object topicList(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            String title,String subtitle,String sort,String order){
        IPage<DtsTopic> iPage = adminTopicService.selectTopicList(page,limit,title,subtitle,sort,order);

        long total = iPage.getTotal();
        List<DtsTopic> records = iPage.getRecords();

        DtsTopic[] items = new DtsTopic[records.size()];
        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }

    //添加专题
    @GetMapping("/create")
    @RequiresPermissions("admin:topic:create")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "专题添加")
    public Object topicCreate(@RequestBody DtsTopic dtsTopic){

        DtsTopic data = adminTopicService.insertTopic(dtsTopic);
        return ResponseUtil.ok(data);
    }

    //专题修改
    @GetMapping("/update")
    @RequiresPermissions("admin:topic:update")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "专题修改")
    public Object topicUpdate(@RequestBody DtsTopic dtsTopic){
        DtsTopic data = adminTopicService.updateTopic(dtsTopic);
        return ResponseUtil.ok(data);
    }

    //专题删除    逻辑删除
    @GetMapping("/delete")
    @RequiresPermissions("admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","专题管理"},button = "专题删除")
    public Object topicDelete(@RequestBody DtsTopic dtsTopic){
        adminTopicService.deleteTopic(dtsTopic.getId());
        return ResponseUtil.ok();
    }

}
